//
//  DataView.m
//  dataPresent
//
//  Created by lurenjia on 2017/6/17.
//  Copyright © 2017年 walker. All rights reserved.
//

#import "DataView.h"
#import "UIView+Extension.h"
#import "AppDelegate.h"
#import "PickResultModel.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define RGBA(r, g, b, a) ([UIColor colorWithRed:(r / 255.0) green:(g / 255.0) blue:(b / 255.0) alpha:a])
#define RGB(r, g, b) RGBA(r,g,b,1)
#define kPickerSize self.timePickView.frame.size

@interface DataView ()<UIPickerViewDelegate,UIPickerViewDataSource,UIGestureRecognizerDelegate>
@property (nonatomic, strong) doneBlock doneBlock;
@property (nonatomic, assign) ChooseStyle chooseStyle;
@property (weak, nonatomic) IBOutlet UIView *buttomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomLayout;
@property (weak, nonatomic) IBOutlet UILabel *showView;
@property (weak, nonatomic) IBOutlet UIPickerView *timePickView;
@property (strong, nonatomic) PickResultModel *pickResultModel;
@end

@implementation DataView

- (instancetype)initWithChooseStyle:(ChooseStyle)chooseStyle CompleteBlock:(doneBlock)completeBlock {
    if (self = [super init]) {
        self = [[[NSBundle mainBundle] loadNibNamed:@"DataView" owner:self options:nil] lastObject];
        self.chooseStyle = chooseStyle;
        [self setupUI];
        if (completeBlock) {
            self.doneBlock = ^(PickResultModel *resultModel) {
                completeBlock(resultModel);
            };
        }
    }
    return self;
}

- (void)setupUI {
    self.buttomView.layer.cornerRadius = 10;
    self.buttomView.layer.masksToBounds = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    self.bottomLayout.constant = -self.height;
    self.backgroundColor = RGBA(0, 0, 0, 0);
    [self layoutIfNeeded];
    switch (self.chooseStyle) {
        case ChooseStyle_Temperature:
            self.showView.text = @"反应温度";
            break;
        case ChooseStyle_Time:
            self.showView.text = @"反应时间";
            break;
        default:
            break;
    }
    _timePickView.delegate = self;
    _timePickView.dataSource = self;
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:self];
    self.pickResultModel = [[PickResultModel alloc]init];
//    [self.showView addSubview:self.timePickView];
//    self.timePickView.showsSelectionIndicator = YES;
}


#pragma mark - Action
-(void)show {
    if (self.chooseStyle == ChooseStyle_Temperature) {
        [self.timePickView selectRow:100 inComponent:0 animated:NO];
    }
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.3 animations:^{
        self.bottomLayout.constant = 10;
        self.backgroundColor = RGBA(0, 0, 0, 0.4);
        [self layoutIfNeeded];
    }];
}

-(void)dismiss {
    [UIView animateWithDuration:.3 animations:^{
        self.bottomLayout.constant = -self.height;
        self.backgroundColor = RGBA(0, 0, 0, 0);
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self removeFromSuperview];
    }];
}

- (IBAction)doneBtnClicked:(UIButton *)sender {
    NSLog(@"%f--%f--%f",self.pickResultModel.hour,self.pickResultModel.minute,self.pickResultModel.second);
    self.doneBlock(self.pickResultModel);
    [self dismiss];
}

-(void)addLabelWithName:(NSArray *)nameArr {
    for (id subView in self.showView.subviews) {
        if ([subView isKindOfClass:[UILabel class]]) {
            [subView removeFromSuperview];
        }
    }
    for (int i=0; i<nameArr.count; i++) {
        CGFloat labelX = kPickerSize.width/(nameArr.count*2)+18+kPickerSize.width/nameArr.count*i;
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(labelX, self.showView.frame.size.height/2-15/2.0, 20, 15)];
        label.text = nameArr[i];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:14];
        label.textColor =  RGB(247, 133, 51);
        label.backgroundColor = [UIColor clearColor];
        [self.showView addSubview:label];
    }
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if( [touch.view isDescendantOfView:self.buttomView]) {
        return NO;
    }
    return YES;
}

#pragma mark - UIPickerViewDelegate && UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    NSInteger count = 0;
    switch (self.chooseStyle) {
        case ChooseStyle_Time:
            [self addLabelWithName:@[@"时",@"分",@"秒"]];
            count = 3;
            break;
        case ChooseStyle_Temperature:
            [self addLabelWithName:@[@"℃"]];
            count = 1;
            break;
        default:
            break;
    }
    return count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (self.chooseStyle) {
        case ChooseStyle_Time:
            return [AppDelegate getInstance].timeArray.count;
            break;
        case ChooseStyle_Temperature:
            return [AppDelegate getInstance].temperatureArray.count;
            break;
        default:
            break;
    }
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *customLabel = (UILabel *)view;
    if (!customLabel) {
        customLabel = [[UILabel alloc]init];
        customLabel.textAlignment = NSTextAlignmentCenter;
        [customLabel setFont:[UIFont systemFontOfSize:17]];
    }
    NSString *title;
    switch (self.chooseStyle) {
        case ChooseStyle_Temperature:
            title = [AppDelegate getInstance].temperatureArray[row];
            break;
        case ChooseStyle_Time:
        {
            title = [AppDelegate getInstance].timeArray[row];
        }
            break;
        default:
            break;
    }
    customLabel.text = title;
    customLabel.textColor = [UIColor blackColor];
    return customLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    switch (self.chooseStyle) {
        case ChooseStyle_Temperature:
            self.pickResultModel.temperature = [AppDelegate getInstance].temperatureArray[row];
            break;
        case ChooseStyle_Time:
        {
            if (component == 0) {
                self.pickResultModel.hour = [[AppDelegate getInstance].timeArray[row] doubleValue];
            }
            else if (component == 1) {
                self.pickResultModel.minute = [[AppDelegate getInstance].timeArray[row] doubleValue];
            }
            else {
            self.pickResultModel.second = [[AppDelegate getInstance].timeArray[row] doubleValue];
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark - getter&&setter

@end
